﻿using System.Linq;
using ClientOverflow.DataTransferObjects;
using ClientOverflow.ViewModels.QuestionsListViewModels;

namespace ClientOverflow.ViewModels.UserProfileViewModels
{
    /// <summary>
    /// Filters questions where the requested user is the owner or posted one of the answers. 
    /// </summary>
    internal class UserQuestionsFilter : IQuestionsFilter
    {
        private readonly User user;

        public UserQuestionsFilter(User user)
        {
            this.user = user;
        }

        public bool IsFittingCrieteria(Question questionToFiler)
        {
            return IsUserQuestion(questionToFiler) ||
                   IsUserAnswer(questionToFiler);
        }

        private bool IsUserAnswer(Question questionToFiler)
        {
            return user.Answers
                .Select(answer => answer.Question)
                .Contains(questionToFiler);
        }

        private bool IsUserQuestion(Question questionToFiler)
        {
            return user.Questions
                .Contains(questionToFiler);
        }
    }
}